//
//  HJNetworkTool.swift
//  Micro-blog Sina
//
//  Created by admin on 16/3/20.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import AFNetworking

enum HJNetworkToolError: Int {
    case AccessTokenEmpty = -1
    case UidEmpty = -2
    case StatusLoading = -3
    case RepeatRefresh = -4
    
    var errorMessage: String {
        get {
            switch self {
            case AccessTokenEmpty: return "access_token 没有值"
            case UidEmpty: return "Uid 没有值"
            case StatusLoading: return "微博数据正在加载...请稍后"
            case .RepeatRefresh: return "伙计，不要上拉刷新又下拉加载"
            }
        }
    }
    
    func error() -> NSError {
        return NSError(domain: "com.hejia.net", code: self.rawValue, userInfo: ["errorMessage" : self.errorMessage])
    }
}

// Network single case inheritance AFN
class HJNetworkTool: NSObject {
    static let sharedInstance:HJNetworkTool = HJNetworkTool()
    private var afnManager: AFHTTPSessionManager
    // Will be the only way to privatization, the external can not be initialized by the constructor.
    private override init() {
        let baseURL = NSURL(string: "https://api.weibo.com/")!
        afnManager = AFHTTPSessionManager(baseURL: baseURL)
        // To support text/plain
        afnManager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        super.init()
    }
    
    
    
    // Access to the OAuth authorized login interface address
    func oauthLoginUrl() -> NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        print("urlString = \(urlString)")
        return NSURL(string: urlString)!
    }
    
    func requestGET(urlString: String, var parameters: [String: AnyObject]?, requestCallBack: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        guard let access_token = HJUserAccountViewModel.sharedInstance.userAccount?.access_token else {
            requestCallBack(result: nil, error: HJNetworkToolError.AccessTokenEmpty.error())
            return
        }
        if parameters == nil {
            parameters = [String : AnyObject]()
        }
        parameters!["access_token"] = access_token
        
        afnManager.GET(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            requestCallBack(result: result as? [String: AnyObject], error: nil)
            }) { (_, error) -> Void in
                requestCallBack(result: nil, error: error)
        }
    }
    
    func requestPOST(urlString: String, parameters: [String: AnyObject]?, requestCallBack: (result: [String: AnyObject]?, error: NSError?) -> ()) {
        afnManager.POST(urlString, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            let dict = result as? [String: AnyObject]
            requestCallBack(result: dict, error: nil)
            }) { (_, error) -> Void in
                requestCallBack(result: nil, error: error)
        }
    }
    
    

}
